<?php

class Nightkiss extends Controller
{
    public function index()
    {
        print 'Hello NightKiss';
    }

    public function getJiedao()
    {
        header('Content-type:text/json');
        $jiedao = array("岱庙街道");

        echo proverb_encode(array("info" => $jiedao));
    }

    public function getShequ()
    {
        header('Content-type:text/json');
        $shequs = array("北关社区", "北新社区", "岱东社区", "岱西社区", "东关村", "东湖社区", "灌庄社区", "花园社区"
        , "金星社区", "丽景社区", "南关社区", "三友社区", "升平社区", "市场社区", "通天社区", "洼子社区", "五马社区"
        , "迎春社区", "迎暄社区", "元宝社区");

        echo proverb_encode(array("info" => $shequs));
    }

    public function getUserLogin()
    {
        header('Content-type:text/json');
        global $db;
        $mobile = empty($_POST['mobile']) ? "" : trim(strip_tags($_POST['mobile']));
        $password2 = empty($_POST['password']) ? "" : trim(strip_tags($_POST['password']));
        $password = md52($password2);
//
        $query = "select user_id from " . tname("user") . " where mobile='" . $mobile . "' and password='" . $password . "'";
        $userId = $db->fetch_first($query);
        if ($userId['user_id'] > 0)
            $json = getUser($userId['user_id']);
        else
            $json = array('user_id' => 0);
        echo proverb_encode($json);
    }

    public function setUserInfo()
    {
        header('Content-type:text/json');
        global $db;
        $user_name = empty($_POST['user_name']) ? "" : trim(strip_tags($_POST['user_name']));
        $password2 = empty($_POST['password']) ? "" : trim(strip_tags($_POST['password']));
        $password = md52($password2);
        $sex = empty($_POST['sex']) ? "男" : trim(strip_tags($_POST['sex']));
        $type = empty($_POST['type']) ? "0" : trim(strip_tags($_POST['type']));
        $weixin = empty($_POST['weixin']) ? "" : trim(strip_tags($_POST['weixin']));
        $mobile = empty($_POST['mobile']) ? "" : trim(strip_tags($_POST['mobile']));
        $idcard = empty($_POST['idcard']) ? "" : trim(strip_tags($_POST['idcard']));
        $place = empty($_POST['place']) ? "" : trim(strip_tags($_POST['place']));
        $shequ = empty($_POST['shequ']) ? "" : trim(strip_tags($_POST['shequ']));
        $jiedao = empty($_POST['jiedao']) ? "" : trim(strip_tags($_POST['jiedao']));
        $danwei = empty($_POST['danwei']) ? "" : trim(strip_tags($_POST['danwei']));
        $zhiwu = empty($_POST['zhiwu']) ? "" : trim(strip_tags($_POST['zhiwu']));
        $ziliao = empty($_POST['ziliao']) ? "" : trim(strip_tags($_POST['ziliao']));
        $techang = empty($_POST['techang']) ? "" : trim(strip_tags($_POST['techang']));
        $gangwei = empty($_POST['gangwei']) ? "" : trim(strip_tags($_POST['gangwei']));
//
//        $logStr = "user_name : " . $_POST['user_name'] . "\r\n";
//        $logStr .= "mobile : " . $_POST['mobile'] . "\r\n";
//        $logStr .= "idcard : " . $_POST['idcard'] . "\r\n";
//        $logStr .= "shequ : " . $_POST['shequ'] . "\r\n";
//        $logStr .= "jiedao : " . $_POST['jiedao'] . "\r\n";
//        $logStr .= "danwei : " . $_POST['danwei'] . "\r\n";
//        $logStr .= "zhiwu : " . $_POST['zhiwu'] . "\r\n";
//        $logStr .= "ziliao : " . $_POST['ziliao'] . "\r\n";
//        $logStr .= "techang : " . $_POST['techang'] . "\r\n";
//        writelog($logStr);

        $query = "select user_id from " . tname("user") . " where mobile='" . $mobile . "'";
        $userIdRaw = $db->fetch_first($query);
        if ($userIdRaw['user_id'] > 0){
            $userId = $userIdRaw['user_id'];
        } else {
            $db->query("insert into " . tname("user") . "(`user_name`, `user_type`, `password`, `password2`, `sex`, `weixin`, `mobile`, `idcard`, `place`, `shequ`, `jiedao`, `danwei`, `zhiwu`, `ziliao`, `techang`, `gangwei`, `created_time`)values('" . $user_name . "','" . $type . "','" . $password . "','" . $password2 . "','" . $sex . "','" . $weixin . "','" . $mobile . "','" . $idcard . "','" . $place . "','" . $shequ . "','" . $jiedao . "','" . $danwei . "','" . $zhiwu . "','" . $ziliao . "','" . $techang . "','" . $gangwei . "','" . time() . "')");
            $userId = $db->insert_id();
        }

        $json = getUser($userId);
        echo proverb_encode($json);
    }

    public function getUserPass()
    {
        header('Content-type:text/json');
        global $db;
        $user_name = empty($_POST['user_name']) ? "" : trim(strip_tags($_POST['user_name']));
        $mobile = empty($_POST['mobile']) ? "" : trim(strip_tags($_POST['mobile']));
        $idcard = empty($_POST['idcard']) ? "" : trim(strip_tags($_POST['idcard']));

        $query = "select user_id,password2 from " . tname("user") . " where user_name='" . $user_name . "' and mobile='" . $mobile . "' and idcard='" . $idcard . "'";
        $userId = $db->fetch_first($query);
        if ($userId['user_id'] > 0)
            $img = array('msg' => 0, 'error' => $userId['password2'], 'timestamp' => time(), 'info' => null);
        else
            $img = array('msg' => 1, 'error' => '输入信息有误', 'timestamp' => time(), 'info' => null);

        echo proverb_encode($img);
    }

    public function getUserInfo()
    {
        header('Content-type:text/json');
        $userId = empty($_POST['userId']) ? 0 : intval(trim(strip_tags($_POST['userId'])));
        $json = getUser($userId);
        echo proverb_encode($json);
    }

    public function getUsersJiFen()
    {
        header('Content-type:text/json');
        $json = getAllUsers(0, "", " order by jifen desc, user_id desc ");
        echo proverb_encode(array("info" => $json));
    }

    public function getJifens()
    {
        header('Content-type:text/json');
        $user_id = empty($_POST['user_id']) ? "0" : trim(strip_tags($_POST['user_id']));
        if ($user_id > 0) {
            $json = getAllJifen(0, $user_id);
            echo proverb_encode(array("info" => $json));
        } else {
            echo null;
        }

    }

    public function getSigns()
    {
        header('Content-type:text/json');

        $year = empty($_POST['year']) ? "2015" : trim(strip_tags($_POST['year']));
        $month = empty($_POST['month']) ? "12" : trim(strip_tags($_POST['month']));
        $user_id = empty($_POST['user_id']) ? "0" : trim(strip_tags($_POST['user_id']));
        if ($user_id > 0) {

            $beginThisMonth = mktime(0, 0, 0, $month, 1, $year);
            $endThisMonth = mktime(23, 59, 59, $month, date('t'), $year);

            $json = getAllSign(0, $user_id, " and created_at > '".$beginThisMonth."' and created_at < '".$endThisMonth."'");
            echo proverb_encode(array("info" => $json));
        } else {
            echo proverb_encode(array("info" => null));
        }

    }

    public function setSign()
    {
        header('Content-type:text/json');
        global $db;

//        s_id` bigint(20) NOT NULL AUTO_INCREMENT,
//  `user_id` bigint(20) NOT NULL DEFAULT '0',
//  `type` tinyint(1) NOT NULL DEFAULT '1',
//  `sindate` varchar(20) NOT NULL DEFAULT '',
//  `created_at` int(11) NOT NULL,  `is_view`

        $year = empty($_POST['year']) ? "2015" : trim(strip_tags($_POST['year']));
        $month = empty($_POST['month']) ? "12" : trim(strip_tags($_POST['month']));
        $day = empty($_POST['day']) ? "0" : trim(strip_tags($_POST['day']));
        $user_id = empty($_POST['user_id']) ? "0" : trim(strip_tags($_POST['user_id']));
        if ($user_id > 0) {
            $date = $year.$month.$day;
            $db->query("insert into " . tname("user_sign") . "(`user_id`, `sindate`, `created_at`)values('" . $user_id . "','" . $date . "','" . time() . "')");

            $beginThisMonth = mktime(0, 0, 0, $month, 1, $year);
            $endThisMonth = mktime(23, 59, 59, $month, date('t'), $year);

            $json = getAllSign(0, $user_id, " and created_at > '".$beginThisMonth."' and created_at < '".$endThisMonth."'");
            echo proverb_encode(array("info" => $json));
        } else {
            echo proverb_encode(array("info" => null));
        }
    }

    public function getArticles()
    {
        header('Content-type:text/json');
        $json = getAllTopics();
        echo proverb_encode(array("info" => $json));
    }

    public function getNews()
    {
        header('Content-type:text/json');
//        $todayTime = strtotime('today');
//        $today = getAllNews(0, " and created_time>=" . $todayTime);
        $otherDay = getAllNews();
        $json = array("today" => array(), "otherDay" => $otherDay);
        echo proverb_encode($json);
    }

    public function getNewsAll()
    {
        header('Content-type:text/json');
        $json = getAllNews();
        echo proverb_encode(array("info" => $json));
    }

    public function getXuexi()
    {
        header('Content-type:text/json');
        $topic_type = empty($_POST['topic_type']) ? "0" : trim(strip_tags($_POST['topic_type']));
        $json = getAllXuexi(0, " and topic_type='" . $topic_type . "'");
        echo proverb_encode(array("info" => $json));
    }

    public function getShici()
    {
        header('Content-type:text/json');
        $json = getAllShici();
        echo proverb_encode(array("info" => $json));
    }

    public function getErxueyizuo()
    {
        header('Content-type:text/json');
        $json = getAllErxueyizuo();
        echo proverb_encode(array("info" => $json));
    }

    public function getRudang()
    {
        header('Content-type:text/json');
        $json = getAllRudang();
        echo proverb_encode(array("info" => $json));
    }

    public function getGonggao()
    {
        header('Content-type:text/json');
        $json = getAllGonggao();
        echo proverb_encode(array("info" => $json));
    }

    public function getFengcai()
    {
        header('Content-type:text/json');
        $json = getAllFengcai();
        echo proverb_encode(array("info" => $json));
    }

    public function getSanhuiyike()
    {
        header('Content-type:text/json');
        $json = getAllSanhuikyike();
        echo proverb_encode(array("info" => $json));
    }

    public function getJingcaihuodong()
    {
        header('Content-type:text/json');
        $json = getAllJingcaihuodong();
        echo proverb_encode(array("info" => $json));
    }

    public function getJizhonghuodongri()
    {
        header('Content-type:text/json');
        $json = getAllJizhonghuodongri();
        echo proverb_encode(array("info" => $json));
    }

    public function getQianshouxingdong()
    {
        header('Content-type:text/json');
        $json = getAllQianshouxingdong();
        echo proverb_encode(array("info" => $json));
    }

    public function getGongzuodongtai()
    {
        header('Content-type:text/json');
        $json = getAllGongzuodongtai();
        echo proverb_encode(array("info" => $json));
    }

    public function getAllService()
    {
        header('Content-type:text/json');
        $json = getServices();
        echo proverb_encode(array("info" => $json));
    }

    public function getAllNeed()
    {
        header('Content-type:text/json');
        $json = getNeeds();
        echo proverb_encode(array("info" => $json));
    }

    public function setNewService()
    {
//        `user_id` bigint(20) NOT NULL DEFAULT '0',
//        `gangwei` varchar(200) NOT NULL DEFAULT '',
//        `gangwei_qita` varchar(200) NOT NULL DEFAULT '',
//        `service_name` varchar(200) NOT NULL DEFAULT '',
//        `service_desc` varchar(200) NOT NULL,
//        `service_time_start` varchar(200) NOT NULL DEFAULT '',
//        `service_time_end` varchar(200) NOT NULL DEFAULT '',
//        `service_place` varchar(200) NOT NULL DEFAULT '',
//        `service_num` int(10) NOT NULL DEFAULT '0',
//        `service_yijian` varchar(200) NOT NULL DEFAULT '',
//        `created_at` int(11) NOT NULL,
//        `update_at` int(11) NOT NULL DEFAULT '0',
//        `is_view` tinyint (1) NOT NULL DEFAULT '0',
        header('Content-type:text/json');
        global $db;
        $user_id = empty($_POST['user_id']) ? "" : trim(strip_tags($_POST['user_id']));
        $service_name = empty($_POST['service_name']) ? "" : trim(strip_tags($_POST['service_name']));
        $service_desc_edit = empty($_POST['service_desc_edit']) ? "" : trim(strip_tags($_POST['service_desc_edit']));
        $place = empty($_POST['place']) ? "" : trim(strip_tags($_POST['place']));
        $gangwei_qita = empty($_POST['gangwei_qita']) ? "" : trim(strip_tags($_POST['gangwei_qita']));
        $num = empty($_POST['num']) ? "" : trim(strip_tags($_POST['num']));
        $time_start = empty($_POST['time_start']) ? "" : trim(strip_tags($_POST['time_start']));
        $time_end = empty($_POST['time_end']) ? "" : trim(strip_tags($_POST['time_end']));
        $gangwei = empty($_POST['gangwei']) ? "" : trim(strip_tags($_POST['gangwei']));
//
        $db->query("insert into " . tname("service") . "(`user_id`, `gangwei`, `gangwei_qita`, `service_name`, `service_desc`, `service_time_start`, `service_time_end`, `service_place`, `service_num`, `created_at`)values('" . $user_id . "','" . $gangwei . "','" . $gangwei_qita . "','" . $service_name . "','" . $service_desc_edit . "','" . $time_start . "','" . $time_end . "','" . $place . "','" . $num . "','" . time() . "')");
        $userId = $db->insert_id();
        setUserJifen($user_id, 3, "提供服务：" . $service_name);
        $db->query("update " . tname("user") . " set jifen=jifen+3 where user_id='" . $user_id . "'");
        $json = getService($userId);
        echo proverb_encode($json);
    }

    public function setNewNeed()
    {
//        `user_id` bigint(20) NOT NULL DEFAULT '0',
//  `need_name` varchar(200) NOT NULL DEFAULT '',
//  `need_desc` varchar(200) NOT NULL DEFAULT '',
//  `need_place` varchar(200) NOT NULL DEFAULT '',
//  `need_time` int(11) NOT NULL DEFAULT '0',
//  `need_num` int(10) NOT NULL DEFAULT '0',
//  `need_yijian` varchar(200) NOT NULL DEFAULT '',
//  `created_at` int(11) NOT NULL,
//  `update_at` int(11) NOT NULL DEFAULT '0',
//  `is_view` tinyint (1) NOT NULL DEFAULT '0',
        header('Content-type:text/json');
        global $db;
        $user_id = empty($_POST['user_id']) ? "" : trim(strip_tags($_POST['user_id']));
        $service_name = empty($_POST['service_name']) ? "" : trim(strip_tags($_POST['service_name']));
        $service_desc_edit = empty($_POST['service_desc_edit']) ? "" : trim(strip_tags($_POST['service_desc_edit']));
        $place = empty($_POST['place']) ? "" : trim(strip_tags($_POST['place']));
        $num = empty($_POST['num']) ? "" : trim(strip_tags($_POST['num']));
        $time_start = empty($_POST['time_start']) ? "" : trim(strip_tags($_POST['time_start']));
        $time_end = empty($_POST['time_end']) ? "" : trim(strip_tags($_POST['time_end']));
//
        $db->query("insert into " . tname("need") . "(`user_id`, `need_name`, `need_desc`, `need_place`, `need_time`, `need_time_end`, `need_num`, `created_at`)values('" . $user_id . "','" . $service_name . "','" . $service_desc_edit . "','" . $place . "','" . $time_start . "','" . $time_end . "','" . $num . "','" . time() . "')");

        $userId = $db->insert_id();
        $json = getNeed($userId);
        echo proverb_encode($json);
    }

    public function setServiceUser()
    {

//        `f_id` bigint(20) NOT NULL AUTO_INCREMENT,
//  `user_id` bigint(20) NOT NULL DEFAULT '0',
//  `s_id` bigint(20) NOT NULL DEFAULT '0',
//  `n_id` bigint(20) NOT NULL DEFAULT '0',
//  `created_at` int(11) NOT NULL,
//  `type` tinyint(1) NOT NULL DEFAULT '0',
//  `is_del` tinyint (1) NOT NULL DEFAULT '0',
        header('Content-type:text/json');
        global $db;
        $user_id = empty($_POST['user_id']) ? "" : trim(strip_tags($_POST['user_id']));
        $s_id = empty($_POST['s_id']) ? "" : trim(strip_tags($_POST['s_id']));

        if ($s_id > 0 && $user_id > 0) {
            $db->query("insert into " . tname("service_need_user") . "(`user_id`, `s_id`, `created_at`)values('" . $user_id . "','" . $s_id . "','" . time() . "')");
            $json = getUser($user_id);
        } else {
            $json = null;
        }

        echo proverb_encode($json);
    }

    public function setServiceFile()
    {
        //`f_id` bigint(20) NOT NULL AUTO_INCREMENT,
        //  `file_id` bigint(20) NOT NULL DEFAULT '0',
        //  `s_id` bigint(20) NOT NULL DEFAULT '0',
        //  `n_id` bigint(20) NOT NULL DEFAULT '0',
        //  `created_at` int(11) NOT NULL,
        //  `is_del` tinyint (1) NOT NULL DEFAULT '0'
        //  `type` tinyint(1) NOT NULL DEFAULT '0',

        header('Content-type:text/json');
        global $db;
        $s_id = empty($_POST['s_id']) ? "" : trim(strip_tags($_POST['s_id']));

        $img = array('hello' => 'world');
        $up = _upload('Filedata');
        $file = $up->insertid;
        if ($file['file_id'] > 0) {
            $db->query("insert into " . tname("service_need_file") . "(`file_id`, `s_id`, `created_at`)values('" . $file['file_id'] . "','" . $s_id . "','" . time() . "')");
            $img = array('msg' => 0, 'error' => '', 'timestamp' => time(), 'info' => getfile($file['file_id']));
        } else {
            $img = array('msg' => 1, 'error' => '', 'timestamp' => time(), 'info' => '');
        }

        echo proverb_encode($img);
    }


    public function setServiceTopic()
    {

        header('Content-type:text/json');
        global $db;
        $topic_id = empty($_POST['topic_id']) ? "0" : trim(strip_tags($_POST['topic_id']));
        $s_id = empty($_POST['s_id']) ? "" : trim(strip_tags($_POST['s_id']));

        if ($s_id > 0 && $topic_id > 0) {
            $db->query("update " . tname("service") . " set topic_id='" . $topic_id . "' where s_id='" . $s_id . "'");
            $json = getService($s_id);
        } else {
            $json = null;
        }

        echo proverb_encode($json);
    }

    public function setNeedUser()
    {
        header('Content-type:text/json');
        global $db;
        $user_id = empty($_POST['user_id']) ? "" : trim(strip_tags($_POST['user_id']));
        $n_id = empty($_POST['n_id']) ? "" : trim(strip_tags($_POST['n_id']));

        if ($n_id > 0 && $user_id > 0) {
            $db->query("insert into " . tname("service_need_user") . "(`user_id`, `n_id`, `created_at`)values('" . $user_id . "','" . $n_id . "','" . time() . "')");
            $json = getUser($user_id);
        } else {
            $json = null;
        }

        echo proverb_encode($json);
    }

    public function setNeedFile()
    {
        header('Content-type:text/json');
        global $db;
        $n_id = empty($_POST['n_id']) ? "" : trim(strip_tags($_POST['n_id']));

        $up = _upload('Filedata');
        $file = $up->insertid;
        if ($file['file_id'] > 0) {
            $db->query("insert into " . tname("service_need_file") . "(`file_id`, `n_id`, `created_at`)values('" . $file['file_id'] . "','" . $n_id . "','" . time() . "')");
            $img = array('msg' => 0, 'error' => '', 'timestamp' => time(), 'info' => getfile($file['file_id']));
        } else {
            $img = array('msg' => 1, 'error' => '', 'timestamp' => time(), 'info' => null);
        }

        echo proverb_encode($img);
    }

    public function setNeedTopic()
    {

        header('Content-type:text/json');
        global $db;
        $topic_id = empty($_POST['topic_id']) ? "0" : trim(strip_tags($_POST['topic_id']));
        $n_id = empty($_POST['n_id']) ? "" : trim(strip_tags($_POST['n_id']));

        if ($n_id > 0 && $topic_id > 0) {
            $db->query("update " . tname("need") . " set topic_id='" . $topic_id . "' where n_id='" . $n_id . "'");
            $json = getNeed($n_id);
        } else {
            $json = null;
        }

        echo proverb_encode($json);
    }

    public function setServiceEnd()
    {
        header('Content-type:text/json');
        global $db;
        $s_id = empty($_POST['s_id']) ? "" : trim(strip_tags($_POST['s_id']));

        $comms = array(
            '我给服务人员好评，态度非常好，感谢这么好的党员同志。',
            '党员同志服务积极，工作过程一丝不苟，还满足了我的其他需求，点赞！',
            '没想到党员同志能提供这么贴心的服务，心里暖暖的！',
            '说到做到，党员同志是我的学习榜样啊！',
            '感谢党员同志为我分忧解难。',
            '好评！微笑服务的党员同志！');

        if ($s_id > 0) {
            $service = getService($s_id);
            $db->query("update " . tname("service") . " set is_view='1', user_ping='" . $comms[rand(0, 5)] . "', user_ping_num='" . rand(4, 5) . "' where s_id='" . $s_id . "'");

            $db->query("update " . tname("user") . " set jifen=jifen+5 where user_id='" . $service['user_id'] . "'");
            setUserJifen($service['user_id'], 5, "提供服务：" . $service['service_name'] . "获得好评");

            $img = array('msg' => 0, 'error' => '', 'timestamp' => time(), 'info' => null);
        } else {
            $img = array('msg' => 1, 'error' => '', 'timestamp' => time(), 'info' => null);
        }
        echo proverb_encode($img);

    }

    public function setNeedEnd()
    {
        header('Content-type:text/json');
        global $db;
        $n_id = empty($_POST['n_id']) ? "" : trim(strip_tags($_POST['n_id']));
        $need_star = empty($_POST['need_star']) ? "5" : trim(strip_tags($_POST['need_star']));
        $need_com = empty($_POST['need_com']) ? "" : trim(strip_tags($_POST['need_com']));

        if ($n_id > 0) {

            $need = getNeed($n_id);

            $db->query("update " . tname("need") . " set is_view='1', need_star='" . $need_star . "', need_com='" . $need_com . "' where n_id='" . $n_id . "'");

            if ($need_star >= 4) {
                setUserJifen($need['user_id'], 5, "参与需求：" . $need['need_name'] . "获得好评");
                $db->query("update " . tname("user") . " set jifen=jifen+5 where user_id='" . $need['user_id'] . "'");
            } elseif ($need_star >= 3) {
                setUserJifen($need['user_id'], 2, "参与需求：" . $need['need_name'] . "获得良好");
                $db->query("update " . tname("user") . " set jifen=jifen+2 where user_id='" . $need['user_id'] . "'");
            }

            $img = array('msg' => 0, 'error' => '', 'timestamp' => time(), 'info' => null);
        } else {
            $img = array('msg' => 1, 'error' => '', 'timestamp' => time(), 'info' => null);
        }
        echo proverb_encode($img);

    }

    public function setBBSTopic()
    {
        //topic_id` bigint(20) NOT NULL AUTO_INCREMENT,
        //  `user_id` bigint(20) NOT NULL DEFAULT '0',
        //  `topic_name` varchar(200) NOT NULL DEFAULT '',
        //  `topic_desc` varchar(200) NOT NULL DEFAULT '',
        //  `message_num` int(10) NOT NULL DEFAULT '0',
        //  `created_at` int(11) NOT NULL,
        //  `update_at` int(11) NOT NULL DEFAULT '0',
        //  `type` tinyint(1) NOT NULL DEFAULT '1',
        //  `is_view` tinyint (1) NOT NULL DEFAULT '0',
        //  `is_del
        header('Content-type:text/json');
        global $db;
        $user_id = empty($_POST['user_id']) ? "0" : trim(strip_tags($_POST['user_id']));
        $type = empty($_POST['type']) ? "1" : trim(strip_tags($_POST['type']));
        $topic_name = empty($_POST['topic_name']) ? "" : trim(strip_tags($_POST['topic_name']));
        $topic_desc = empty($_POST['topic_desc']) ? "" : trim(strip_tags($_POST['topic_desc']));
        $db->query("insert into " . tname("bbs_topic") . "(`user_id`, `topic_name`, `topic_desc`, `type`, `created_at`)values('" . $user_id . "','" . $topic_name . "','" . $topic_desc . "','" . $type . "','" . time() . "')");
        $json = getBBSTopic($db->insert_id());
        echo proverb_encode($json);
    }

    public function getBBSTopic()
    {
        header('Content-type:text/json');
        $type = empty($_POST['type']) ? "1" : trim(strip_tags($_POST['type']));
        $json = getAllBBSTopic(0, " and type='" . $type . "'");
        echo proverb_encode(array("info" => $json));
    }

    public function getBBSMessage()
    {
        header('Content-type:text/json');
        $topic_id = empty($_POST['topic_id']) ? "" : trim(strip_tags($_POST['topic_id']));
        $message_id = empty($_POST['message_id']) ? "0" : trim(strip_tags($_POST['message_id']));
        $page = empty($_POST['page']) ? "0" : trim(strip_tags($_POST['page']));
        $con = " and topic_id='" . $topic_id . "'";
        if ($message_id > 0) {
            $con .= " and message_id>'" . $message_id . "'";
        }
//        writelog($con."\r\n");
        $json = getAllBBSMessage($page, $topic_id, $con);
        echo proverb_encode(array("info" => $json));
    }

    public function setBBSMessage()
    {

//        `message_id` bigint(20) NOT NULL AUTO_INCREMENT,
//  `user_id` bigint(20) NOT NULL DEFAULT '0',
//  `topic_id` bigint(20) NOT NULL DEFAULT '0',
//  `type` tinyint(1) NOT NULL DEFAULT '1',
//  `raw_text` TEXT NOT NULL DEFAULT '',
//  `file_id` bigint(20) NOT NULL DEFAULT '0',
//  `mp3_id` bigint(20) NOT NULL DEFAULT '0',
//  `created_at` int(11) NOT NULL,
//  `is_view` tinyint (1) NOT NULL DEFAULT '0',
//  `is_del` tinyint (1) NOT NULL DEFAULT '0'

        header('Content-type:text/json');
        global $db;
        $position_id = empty($_POST['position_id']) ? "0" : trim(strip_tags($_POST['position_id']));
        $topic_id = empty($_POST['topic_id']) ? "" : trim(strip_tags($_POST['topic_id']));
        $user_id = empty($_POST['user_id']) ? "" : trim(strip_tags($_POST['user_id']));
        $type = empty($_POST['type']) ? "" : trim(strip_tags($_POST['type']));
        $raw_text = empty($_POST['raw_text']) ? "" : trim(strip_tags($_POST['raw_text']));
        $file_id = 0;
        $mp3_id = 0;
        if ($type == 2) {

            $up = _upload('Filedata');
            $file = $up->insertid;
            if ($file['file_id'] > 0) {
                $file_id = $file['file_id'];
                $img = array('msg' => 0, 'error' => '', 'timestamp' => time(), 'info' => getfile($file['file_id']));
            } else {
                $img = array('msg' => 1, 'error' => '', 'timestamp' => time(), 'info' => null);
            }

        }

        $db->query("insert into " . tname("bbs_message") . "(`user_id`, `topic_id`, `type`, `raw_text`, `file_id`, `mp3_id`, `created_at`)values('" . $user_id . "','" . $topic_id . "','" . $type . "','" . $raw_text . "','" . $file_id . "','" . $mp3_id . "','" . time() . "')");
        $message_id = $db->insert_id();
        if ($message_id > 0) {
            $db->query("update " . tname("bbs_topic") . " set message_num=message_num+1, update_at='" . time() . "' where topic_id='" . $topic_id . "'");
            $json = getBBSMessage($message_id);
            $json['position_id'] = $position_id;
        } else
            $json = null;
        echo proverb_encode($json);
    }

    public function setUserJifen()
    {
        header('Content-type:text/json');
        global $db;

        $user_id = empty($_POST['user_id']) ? "" : trim(strip_tags($_POST['user_id']));
        $jifen = empty($_POST['jifen']) ? "0" : trim(strip_tags($_POST['jifen']));
        $name = empty($_POST['name']) ? "" : trim(strip_tags($_POST['name']));
        if ($user_id > 0) {
            setUserJifen($user_id, $jifen, $name);
            $db->query("update " . tname("user") . " set jifen=jifen+" . $jifen . " where user_id='" . $user_id . "'");
            $jifenCount = $jifen . "," . getUserJifen($user_id);
            $img = array('msg' => 0, 'error' => $jifenCount, 'timestamp' => time(), 'info' => null);
        } else {
            $img = array('msg' => 1, 'error' => '', 'timestamp' => time(), 'info' => null);
        }
        echo proverb_encode($img);

    }

    public function getSixianghuibao()
    {
        header('Content-type:text/json');
        $user_id = empty($_POST['user_id']) ? "0" : trim(strip_tags($_POST['user_id']));
        $json = getAllSixianghuibao(0, " and user_id='" . $user_id . "'");
        echo proverb_encode(array("info" => $json));
    }

    public function setSixianghuibao()
    {
        header('Content-type:text/json');
        global $db;
        $user_id = empty($_POST['user_id']) ? "0" : trim(strip_tags($_POST['user_id']));
        $topic_name = empty($_POST['topic_name']) ? "" : trim(strip_tags($_POST['topic_name']));
        $topic_desc = empty($_POST['topic_desc']) ? "" : trim(strip_tags($_POST['topic_desc']));
        $db->query("insert into " . tname("sixianghuibao") . "(`user_id`, `topic_name`, `topic_desc`, `created_time`)values('" . $user_id . "','" . $topic_name . "','" . $topic_desc . "','" . time() . "')");
        $json = getSixianghuibao($db->insert_id());
        echo proverb_encode($json);
    }

    public function getXiangshanghuibao()
    {
        header('Content-type:text/json');
        $user_id = empty($_POST['user_id']) ? "0" : trim(strip_tags($_POST['user_id']));
//        $json = getAllXiangshanghuibao(0, " and user_id='" . $user_id . "'");
        $json = getAllXiangshanghuibao(0);
        echo proverb_encode(array("info" => $json));
    }

    public function setXiangshanghuibao()
    {
        header('Content-type:text/json');
        global $db;
        $user_id = empty($_POST['user_id']) ? "0" : trim(strip_tags($_POST['user_id']));
        $topic_name = empty($_POST['topic_name']) ? "" : trim(strip_tags($_POST['topic_name']));
        $topic_desc = empty($_POST['topic_desc']) ? "" : trim(strip_tags($_POST['topic_desc']));
        $db->query("insert into " . tname("xiangshanghuibao") . "(`user_id`, `topic_name`, `topic_desc`, `created_time`)values('" . $user_id . "','" . $topic_name . "','" . $topic_desc . "','" . time() . "')");
        $json = getXiangshanghuibao($db->insert_id());
        echo proverb_encode($json);
    }

    public function getFeedBack()
    {
        header('Content-type:text/json');
        $user_id = empty($_POST['user_id']) ? "0" : trim(strip_tags($_POST['user_id']));
        $json = getAllFeedback(0, $user_id);
        echo proverb_encode(array("info" => $json));
    }

    public function setFeedBack()
    {
        //f_id` bigint(20) NOT NULL AUTO_INCREMENT,
        //  `re_id` bigint(20) NOT NULL DEFAULT '0',
        //  `user_id` bigint(20) NOT NULL DEFAULT '0',
        //  `email` varchar(32) NOT NULL,
        //  `raw_text` varchar(200) NOT NULL,
        //   `raw_text_re` varchar(200) NOT NULL,
        //  `file_id` bigint(20) NOT NULL DEFAULT '0',
        //  `created_at` int(11) NOT NULL,
        //  `is_view
        header('Content-type:text/json');
        global $db;
        $user_id = empty($_POST['user_id']) ? "0" : trim(strip_tags($_POST['user_id']));
        $raw_text = empty($_POST['desc']) ? "" : trim(strip_tags($_POST['desc']));

        $db->query("insert into " . tname("feedback") . "(`user_id`, `email`, `raw_text`, `raw_text_re`, `file_id`, `created_at`)values('" . $user_id . "','','" . $raw_text . "','','0','" . time() . "')");
        $f_id = $db->insert_id();
        if ($f_id > 0) {
            $json = getFeedBack($f_id);
        } else
            $json = null;
        echo proverb_encode($json);

    }

    public function getWeimengxiang()
    {
        header('Content-type:text/json');
        $user_id = empty($_POST['user_id']) ? "0" : trim(strip_tags($_POST['user_id']));
        $json = getAllWeimengxiang(0, $user_id);
        echo proverb_encode(array("info" => $json));
    }

    public function setWeimengxiang()
    {
        //f_id` bigint(20) NOT NULL AUTO_INCREMENT,
        //  `re_id` bigint(20) NOT NULL DEFAULT '0',
        //  `user_id` bigint(20) NOT NULL DEFAULT '0',
        //  `email` varchar(32) NOT NULL,
        //  `raw_text` varchar(200) NOT NULL,
        //   `raw_text_re` varchar(200) NOT NULL,
        //  `file_id` bigint(20) NOT NULL DEFAULT '0',
        //  `created_at` int(11) NOT NULL,
        //  `is_view
        header('Content-type:text/json');
        global $db;
        $user_id = empty($_POST['user_id']) ? "0" : trim(strip_tags($_POST['user_id']));
        $raw_text = empty($_POST['desc']) ? "" : trim(strip_tags($_POST['desc']));

        $db->query("insert into " . tname("weimengxiang") . "(`user_id`, `email`, `raw_text`, `raw_text_re`, `file_id`, `created_at`)values('" . $user_id . "','','" . $raw_text . "','','0','" . time() . "')");
        $f_id = $db->insert_id();
        if ($f_id > 0) {
            $json = getWeimengxiang($f_id);
        } else
            $json = null;
        echo proverb_encode($json);

    }

    public function getSearch()
    {
        header('Content-type:text/json');
        $text = empty($_POST['text']) ? "" : trim(strip_tags($_POST['text']));
        if(!empty($text)) {
            $json = getAllSearch(0, " and topic_name like '%" . $text . "%'");
        } else
            $json = null;
        echo proverb_encode(array("info" => $json));
    }

    public function setHuibaoFile()
    {
        //`f_id` bigint(20) NOT NULL AUTO_INCREMENT,
        //  `file_id` bigint(20) NOT NULL DEFAULT '0',
        //  `s_id` bigint(20) NOT NULL DEFAULT '0',
        //  `n_id` bigint(20) NOT NULL DEFAULT '0',
        //  `created_at` int(11) NOT NULL,
        //  `is_del` tinyint (1) NOT NULL DEFAULT '0'
        //  `type` tinyint(1) NOT NULL DEFAULT '0',

        header('Content-type:text/json');
        global $db;
        $s_id = empty($_POST['s_id']) ? 0 : trim(strip_tags($_POST['s_id']));
        $type = empty($_POST['type']) ? 0 : trim(strip_tags($_POST['type']));

        $img = array('hello' => 'world');
        $up = _upload('Filedata');
        $file = $up->insertid;
        if ($file['file_id'] > 0) {
            $db->query("insert into " . tname("huibao_file") . "(`file_id`, `s_id`, `type`, `created_at`)values('" . $file['file_id'] . "','" . $s_id . "','".$type."','" . time() . "')");
            $img = array('msg' => 0, 'error' => '', 'timestamp' => time(), 'info' => getfile($file['file_id']));
        } else {
            $img = array('msg' => 1, 'error' => '', 'timestamp' => time(), 'info' => '');
        }

        echo proverb_encode($img);
    }

}